package com.example.ioc.factory;

import com.example.ioc.proxy.CglibProxy;
import com.example.ioc.proxy.JdkProxy;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

public class BeanProxyFactory {
    private Object targetObj;

    public BeanProxyFactory(Object targetObj) {
        this.targetObj = targetObj;
    }

    public Object getBean(){
        Class<?>[] interfaces = targetObj.getClass().getInterfaces();
        Object obj=null;
        if (interfaces.length>0){
            JdkProxy jdkProxy=new JdkProxy(targetObj);
            obj=Proxy.newProxyInstance(
                    targetObj.getClass().getClassLoader(),
                    interfaces,
                    jdkProxy);
        }else {
            CglibProxy cglibProxy=new CglibProxy(targetObj);
            obj= Enhancer.create(targetObj.getClass(),cglibProxy);
        }
        return obj;
    }
}
